<template>
  <div class="login-wrap">
    <el-form label-position="left" :model="ruleForm" :rules="rules" size="medium" ref="loginForm" class="login-container">
      <h2 class="title">用户登录</h2>
      <el-form-item prop="username">
        <el-input type="text" v-model="ruleForm.username" prefix-icon="el-icon-user"
                  auto-complete="off" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" prefix-icon="el-icon-lock"
                  show-password auto-complete="off" placeholder="请输入密码" @keyup.enter.native="_login"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="14">
          <el-form-item prop="code">
            <el-input type="text" v-model="ruleForm.code" prefix-icon="el-icon-s-grid"
                      auto-complete="off" placeholder="图形验证码" @keyup.enter.native="_login"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" class="code-box">
          <img :src="ruleForm.codeImg" alt="" class="code-img" @click="_getCode()">
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" size="medium" style="width:100%;" @click.native.prevent="_login">登录</el-button>
      </el-form-item>
    </el-form>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 TINY-CLOUD All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
  import md5 from "@/utils/md5";
  import {getCode} from "@/api/login";

  export default {
    name: "Login",
    data() {
      return {
        ruleForm: {
          username: 'admin', // 用户名
          password: '123456a?', // 密码
          code: '', // 输入的验证码
          codeImg: '', // 返回的验证码base64
          uuid: '' // 验证码的key
        },
        loginForm: {
          username: '',
          password: '',
          code: '',
          uuid: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在3到15个字符', trigger: 'blur' }
          ],
          password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
          code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
        },
      }
    },
    computed: {
      redirect: function () {
        return this.$route.query && this.$route.query.redirect
      }
    },
    mounted() {
      this._getCode();
    },
    methods: {
      _login() {
        this.$refs['loginForm'].validate(valid => {
          if (valid) {
            this.loginForm.password = md5.MD5(this.ruleForm.password);
            this.loginForm.username = this.ruleForm.username;
            this.$store.dispatch('user/_login', this.loginForm).then(res => {
              if (res.code !== 0) {
                // 刷新验证码
                this._getCode();
              } else {
                this.$router.push({path: this.redirect || '/'}).catch(err=>{});
              }
            }).catch(error => {
              // 刷新验证码
              this._getCode();
            })
          } else {
            return false
          }
        });

      },
      _getCode() {
        getCode().then(res => {
          if (res.code === 0) {
            this.ruleForm.codeImg = 'data:image/gif;base64,' + res.data.img;
            this.ruleForm.uuid = res.data.uuid;
          } else {
            this.$message.error("获取验证码失败，请联系后台管理员");
          }
        }).catch(error=>{
          this.$message.error(error);
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  .login-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background-image: url();
    background-size: cover;
  }
  .login-container {
    border-radius: 5px;
    width: 350px;
    padding: 30px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  }
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
  .code-box {
    text-align: right;
  }
  .code-img {
    height: 36px;
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #454b61;
    font-family: Arial,serif;
    font-size: 12px;
    letter-spacing: 1px;
  }
</style>

